﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using javanet_demo.Models;
using System.Diagnostics;
using javanet_demo.demo;
using JavaNET.mvc;

namespace javanet_demo.Controllers
{
    public class ProjectController : Controller
    {
        [JavaNETFacade]
        private ProjectFacade projectFacade;

        public ActionResult Index()
        {
            List<Project> projects = null;
            try
            {
                projects = projectFacade.findAllProjects();
            }
            catch (Exception e)
            {
                Debug.Write(e);
                projects = new List<Project>();
            }
            ProjectListModel listModel = new ProjectListModel();

            listModel.projects = projects;

            return View(listModel);
        }

        public ActionResult Change(long selectedId)
        {
            Project selectedProject = null;
            if (selectedId > 0)
                selectedProject = projectFacade.findProjectById(selectedId);
            else
                selectedProject = new Project();
            Debug.WriteLine(selectedProject.code);
            ChangeProjectModel model = new ChangeProjectModel();
            model.Code = selectedProject.code;
            model.NameShort = selectedProject.nameShort;
            model.NameLong = selectedProject.nameLong;
            model.milestones = selectedProject.milestones;
            model.selectedProject = selectedProject;
            HttpContext.Session.Add("selectedProject", selectedProject);
            return View(model);
        }

        public ActionResult ChangeMilestone(Milestone p)
        {
            Debug.WriteLine(p.code);
            return View();
        }

        [HttpPost]
        public ActionResult ChangeMilestone(ChangeMilestoneModel model)
        {
            Project project = (Project)HttpContext.Session["selectedProject"];
            Milestone milestone = new Milestone();
            milestone.code = model.Code;
            milestone.name = model.Name;
            milestone.date = model.Date;
            projectFacade.addMilestoneToProject(project, milestone);
            return RedirectToAction("Index");
        }

        [HttpPost]
        [ButtonClick(Name = "SaveProject")]
        public ActionResult SaveProject(ChangeProjectModel model)
        {
            Project project = (Project)HttpContext.Session["selectedProject"];
            project.code = model.Code;
            project.nameShort = model.NameShort;
            project.nameLong = model.NameLong;
            projectFacade.saveProject(project);
            return RedirectToAction("Index");
        }

        [HttpPost]
        [ButtonClick(Name = "AddMilestone")]
        public ActionResult AddMilestone(ChangeProjectModel model)
        {
            return RedirectToAction("ChangeMilestone", new Milestone());
        }

        [HttpPost]
        [ButtonClick(Name = "AddProject")]
        public ActionResult AddProject()
        {
            return RedirectToAction("Change", new { selectedId = 0 });
        }

    }
}
